package ca.seia.comp6521.a2.util;

import static org.junit.Assert.assertEquals;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class MemoryCalculatorTestCase {
	
	private static final String VM_BIT_SIZE_PROPERTY = "sun.arch.data.model";
	private static final int MB = 1048576;
	private static final int NBR_PRIMITIVE_INTS_FOR_1MEG = 262144;
	private static final int NBR_INTEGERS_IN32VM_FOR_1MEG = 65536;
	private static final int NBR_INTEGERS_IN64M_FOR_1MEG = 32768;
	
	
	private int originalVMBitSize;
	
	@Before
	public void setUp() throws Exception {
		originalVMBitSize = Integer.parseInt(System.getProperty(VM_BIT_SIZE_PROPERTY));
	}
	
	@After
	public void cleanUp() throws Exception {
		System.setProperty(VM_BIT_SIZE_PROPERTY, String.valueOf(originalVMBitSize));
	}

	@Test
	public void testNumberOfPrimitiveIntsFittingInOneMegabyte() {
		assertEquals(NBR_PRIMITIVE_INTS_FOR_1MEG,MemoryCalculator.getNumberOfPrimitiveInt(1));
	}
	
	@Test
	public void testNumberOfIntegersFittingInOneMegabyteWith32BitVM() {
		setVMTo32Bit();
		assertEquals(NBR_INTEGERS_IN32VM_FOR_1MEG,MemoryCalculator.getNumberOfIntegers(1));
	}
	
	@Test
	public void testNumberOfIntegersFittingInOneMegabyteWith64BitVM() {
		setVMTo64Bit();
		assertEquals(NBR_INTEGERS_IN64M_FOR_1MEG,MemoryCalculator.getNumberOfIntegers(1));
	}
	
	
	@Test
	public void testMemoryVm64BitSize() {
		setVMTo64Bit();
		assertEquals(64, MemoryCalculator.getVMBitSize());
	}
	
	@Test
	public void testMemoryVm32BitSize() {
		setVMTo32Bit();
		assertEquals(32, MemoryCalculator.getVMBitSize());
	}
	

	private void setVMTo32Bit() {
		System.setProperty(VM_BIT_SIZE_PROPERTY, "32");
	}
	
	private void setVMTo64Bit() {
		System.setProperty(VM_BIT_SIZE_PROPERTY, "64");
	}

}
